package com.workflow.action;

import java.util.Set;

import com.base.service.PermissionService;
import com.common.bean.Flowtemplatebranch;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Exitdept;
import com.workflow.bean.Exitdeptdetail;
import com.workflow.service.ExitdeptService;
import com.workflow.service.ExitdeptdetailService;

public class ExitdeptdetailManagerAction extends AbstractActionSupport
		implements Preparable {
	private static final long serialVersionUID = -6586032708693383847L;

	private ExitdeptdetailService exitdeptdetailService;
	
	private ExitdeptService exitdeptService;
	
	private PermissionService permissionService;
	
	private Exitdeptdetail exitdeptdetail;
	
	private Exitdept exitdept;
	
	private String exitno;
	
	private String exitdetailno;
	
	private PagerModel pm;
	
	private Set cardsubtypelist;
	
	public void prepare() throws Exception {
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddExitdeptdetail() throws Exception {
		exitdept = exitdeptService.findExitdept(exitno);
		cardsubtypelist = exitdept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addExitdeptdetail() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		exitdept = exitdeptService.findExitdept(exitno);
		exitdeptdetail.setExitdept(exitdept);
		
		Object obj = null;
		long ruleno = exitdept.getFlowtemplatedetail().getRule().getRuleno();
		String staffcode = personnel.getStaffcode();
		
		obj = permissionService.findPersonneltorulelink(ruleno, staffcode);		
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		if(StringToken.DICT_FLOWBRANCH_YES.equals(exitdept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = exitdept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					exitdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(exitdeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						exitdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
			}
		}
		
		exitdeptdetailService.addExitdetailno(exitdeptdetail);
		
		return SUCCESS;
	}
	
	public String delExitdeptdetail() throws Exception {
		//需要删除相应的卡信息状态
		exitdeptdetailService.removeExitdetailno(exitdetailno);
		return SUCCESS;
	}
	
	public String findAllExitdeptdetail() throws Exception {
		exitdept = exitdeptService.findExitdept(exitno);
		pm = exitdeptdetailService.findExitdetailnoForPager(exitno);
		return SUCCESS;
	}
	
	public String findExitdeptdetail() throws Exception {
		exitdeptdetail = exitdeptdetailService.findExitdetailno(exitdetailno);
		return SUCCESS;
	}
	
	public String getExitdeptdetailForUpdate() throws Exception {
		exitdeptdetail = exitdeptdetailService.findExitdetailno(exitdetailno);
		exitdept = exitdeptService.findExitdept(exitdeptdetail.getExitdept().getExitno());
		cardsubtypelist = exitdept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String updateExitdeptdetail() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		exitdept = exitdeptService.findExitdept(exitdeptdetail.getExitdept().getExitno());

		Object obj = null;
		long ruleno = exitdept.getFlowtemplatedetail().getRule().getRuleno();
		String staffcode = personnel.getStaffcode();
		
		obj = permissionService.findPersonneltorulelink(ruleno, staffcode);		
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		if(StringToken.DICT_FLOWBRANCH_YES.equals(exitdept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = exitdept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					exitdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(exitdeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						exitdeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
			}
		}
		
		exitdeptdetailService.updateExitdetailno(exitdeptdetail);
		return SUCCESS;
	}

	/**
	 * @return the cardsubtypelist
	 */
	public Set getCardsubtypelist() {
		return cardsubtypelist;
	}

	/**
	 * @param cardsubtypelist the cardsubtypelist to set
	 */
	public void setCardsubtypelist(Set cardsubtypelist) {
		this.cardsubtypelist = cardsubtypelist;
	}

	/**
	 * @return the exitdept
	 */
	public Exitdept getExitdept() {
		return exitdept;
	}

	/**
	 * @param exitdept the exitdept to set
	 */
	public void setExitdept(Exitdept exitdept) {
		this.exitdept = exitdept;
	}

	/**
	 * @return the exitdeptdetail
	 */
	public Exitdeptdetail getExitdeptdetail() {
		return exitdeptdetail;
	}

	/**
	 * @param exitdeptdetail the exitdeptdetail to set
	 */
	public void setExitdeptdetail(Exitdeptdetail exitdeptdetail) {
		this.exitdeptdetail = exitdeptdetail;
	}

	/**
	 * @return the exitdeptdetailService
	 */
	public ExitdeptdetailService getExitdeptdetailService() {
		return exitdeptdetailService;
	}

	/**
	 * @param exitdeptdetailService the exitdeptdetailService to set
	 */
	public void setExitdeptdetailService(ExitdeptdetailService exitdeptdetailService) {
		this.exitdeptdetailService = exitdeptdetailService;
	}

	/**
	 * @return the exitdeptService
	 */
	public ExitdeptService getExitdeptService() {
		return exitdeptService;
	}

	/**
	 * @param exitdeptService the exitdeptService to set
	 */
	public void setExitdeptService(ExitdeptService exitdeptService) {
		this.exitdeptService = exitdeptService;
	}

	/**
	 * @return the exitdetailno
	 */
	public String getExitdetailno() {
		return exitdetailno;
	}

	/**
	 * @param exitdetailno the exitdetailno to set
	 */
	public void setExitdetailno(String exitdetailno) {
		this.exitdetailno = exitdetailno;
	}

	/**
	 * @return the exitno
	 */
	public String getExitno() {
		return exitno;
	}

	/**
	 * @param exitno the exitno to set
	 */
	public void setExitno(String exitno) {
		this.exitno = exitno;
	}

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}
	
	
}
